import { createApp } from 'vue';
import App from './App.vue';
import router from './router/index';
import store from './store/index';
import UI from './utils/index.js';
import ElementUI from 'element-plus';
// import 'element-plus/theme-chalk/index.css';
import locale from 'element-plus/lib/locale/lang/zh-cn'
// import ElementUI from 'element3';
// import 'element3/lib/theme-chalk/index';
import NavMenu from './components/NavMenu.vue';
import { uInfo } from '@/api/user_api';
import { RouteRecordRaw } from '_vue-router@4.0.11@vue-router';


const whiteList = ['/login', '/register'];

/**
 * @导航守卫，检查路由元字段，是否配置登录态
 */
 router.beforeEach((to, from, next) => {
	UI.setStorage('HISTORY_PATH', from.path);
	if(to.matched.some(record => whiteList.includes(record.path))) {  // 白名单无需检测登录态
		next();
	}else {
		const uid: string = store.state.uinfo.id;
		if(uid) {
			next();
		}else {
			uInfo().then(response => {
				let res = response.data;
				if(res.s) {
					store.dispatch('SET_UINFO', res.d);
					store.dispatch('SET_MENUS').then(res => {
						res.forEach((item: RouteRecordRaw) => {
							router.addRoute('Index', item);  // 动态添加可访问路由表
						})
					})
					next({ ...to, replace: true })
				} else {
					next({
						path: '/login'
					})
				}
			})
		}

	}
});
const app = createApp(App);
app.use(router)
	.use(store)
  .use(ElementUI, {locale})
	.component('nav-menu', NavMenu)
	.mount('#app');